﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Threading.Tasks;
using PublishNews.Entities;

namespace PublishNews.Repositories
{
    public class NewsRepository : BaseRepository<News>, INewsRepository
    {
        public async Task<List<News>> GetNewsByCategoryAndLocation(Guid categoryId, Guid locationId)
        {
            using (var context = Context)
            {
                return await context.Set<News>().Where(x => x.CategoryId == categoryId && x.LocationId == locationId).ToListAsync();
            }
        }

        public async Task<List<News>> GetNewsByCategoryAndLocation(Guid categoryId, Guid locationId, int pageIndex, int pageSize)
        {
            using (var context = Context)
            {
                return await context.Set<News>().Where(x => x.CategoryId == categoryId && x.LocationId == locationId)
                    .OrderByDescending(x => x.CreatedDate)
                    .Skip(pageIndex * pageSize).Take(pageSize).ToListAsync();
            }
        }


        public async Task<List<News>> GetNewsByUserId(string userId)
        {
            using (var context = Context)
            {
                return await context.Set<News>().Where(x => x.UserId == userId).ToListAsync();
            }
        }


        public async Task<List<News>> GetNewsByUserId(string userId, Guid categoryId, Guid locationId)
        {
            using (var context = Context)
            {
                return await context.Set<News>().Where(x => x.UserId == userId && x.CategoryId == categoryId && x.LocationId == locationId).ToListAsync();
            }
        }
    }
}